<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./swiper-5.4.5/css/swiper.css" />
    <style>
      .swiper-container {
        width: 600px;
        height: 300px;
      }
      .swiper-slide img {
        width: 100%;
      }
    </style>
    <script src="./swiper-5.4.5/js/swiper.js"></script>
    <script src="./js/jQuery.js"></script>
  </head>
  <body>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <!-- <div class="swiper-slide"><img src="https://t9.baidu.com/it/u=3363001160,1163944807&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1595383877&t=3eb7de6dafee9feac9418b6d21212e7e" alt=""></div>
        <div class="swiper-slide"><img src="https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1595383877&t=574a1515532578558976677846615151" alt=""></div>
        <div class="swiper-slide"><img src="https://t9.baidu.com/it/u=1307125826,3433407105&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1595383877&t=705814640799d6ac48aa1c2b9fa8dd72" alt=""></div> -->
      </div>
      <!-- 如果需要分页器 -->
      <div class="swiper-pagination"></div>

      <!-- 如果需要导航按钮 -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>

      <!-- 如果需要滚动条 -->
      <div class="swiper-scrollbar"></div>
    </div>
    <script>
      // 如果你的script不是放到页面的底部一定记得要加入口偶函数
      $.ajax({
        // 请求接口地址
        url: 'http://www.liulongbin.top:3005/api/getlunbo',
        type: 'get', // 请求方式 默认就get
        success: function (data) {
          // 成功的回调函数 data就是后台给我的数据
          // console.log(data, 1111)
          if (data.status !== 0) return alert('请求失败');
          // 成功的处理逻辑
          // console.log(data.message)
          var str = '';
          data.message.forEach(function (item, index) {
            // 字符串累加
            str += `<div class="swiper-slide"><img src="${item.img}" alt=""></div>`;
          });
          // console.log(str)
          // 将拼接好的字符串放到页面中去
          // $('.swiper-wrapper').html(str)
          document.querySelector('.swiper-wrapper').innerHTML = str;
          // 再来初始化轮播图
          var mySwiper = new Swiper('.swiper-container', {
            // direction: 'vertical', // 垂直切换选项
            loop: true, // 循环模式选项
            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
          });
        }
      });
    </script>
  </body>
</html>
